package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.util.ListIterator;

import javax.swing.ImageIcon;
import javax.swing.JComponent;

@SuppressWarnings("serial")
public class DrawPerson extends JComponent implements ActionListener {
	private Image peopleImage, zombieImage;
	public static final int ICON_SCALING = 15;

	public DrawPerson() {
		peopleImage = new ImageIcon("Images/man.png").getImage().getScaledInstance(ICON_SCALING, ICON_SCALING, 0);
		zombieImage = new ImageIcon("Images/zombie.png").getImage().getScaledInstance(ICON_SCALING, ICON_SCALING, 0);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		repaint();

	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		field(g);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		drawPeoples(g2d);
		drawZombies(g2d);
		paintComponents(g2d);
	}

	private void drawPeoples(Graphics2D g) {
		for (Person people : ListOfPerson.getPeoplesList()) {
			people.setSpeed(1.5);
			AffineTransform at = new AffineTransform();
			at.translate(people.getX1(), people.getY1());
			// at.rotate(people.getRotInRad());
			g.drawImage(peopleImage, at, null);

			g.setColor(Color.BLACK);

			g.drawString("" + (ListOfPerson.getPeoplesList().indexOf(people) + 1), (int) people.getX1() - ICON_SCALING
					/ 3, (int) people.getY1() + ICON_SCALING / 3);

			people.move();
			people.moveRandomWithRotation();
		}
	}

	private void drawZombies(Graphics2D g) {

		for (int i = 0; i < ListOfPerson.getLiftOfZombies().size(); i++) {
			Person zombie = ListOfPerson.getLiftOfZombies().get(i);
			AffineTransform at2 = new AffineTransform();
			at2.translate(zombie.getX1(), zombie.getY1());
			// at2.rotate(people.getRotInRad());
			g.drawImage(zombieImage, at2, null);

			g.setColor(Color.BLACK);

			g.drawString("" + (ListOfPerson.getLiftOfZombies().indexOf(zombie) + 1), (int) zombie.getX1()
					- ICON_SCALING / 3, (int) zombie.getY1() + ICON_SCALING / 3);
			zombie.move();
			zombie.goAfterPeople(zombie);

		}

	}

	protected void field(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setColor(Color.white);
		g.fillRect(10, 5, Main.WIDTH_OF_FIELD, Main.HEIGHT_OF_FIELD);
		g2d.setColor(Color.black);
		g2d.drawRect(10, 5, Main.WIDTH_OF_FIELD - 11, Main.HEIGHT_OF_FIELD - 6);
	}

}
